package com.alex;

import android.app.Activity;
import android.util.Log;
import android.view.View;

import com.anythink.china.api.CustomAdapterDownloadListener;
import com.anythink.nativead.api.ATNativePrepareInfo;
import com.anythink.nativead.api.NativeAdInteractionType;
import com.anythink.nativead.unitgroup.api.CustomNativeAd;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTFeedAd;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationExpressRenderListener;

import java.util.HashMap;
import java.util.Map;

public class AlexGromoreExpressNativeAd extends CustomNativeAd {
    private static final String TAG = AlexGromoreExpressNativeAd.class.getSimpleName();
    TTFeedAd mTTFeedAd;
    View expressFeedView;

    double videoProgress;
    boolean isDownloadStart = false;

    public AlexGromoreExpressNativeAd(TTFeedAd ttFeedAd, float width, float height) {
        this.mTTFeedAd = ttFeedAd;

        bindListener();
        setAdData(false);

        Map<String, Object> networkInfoMap = new HashMap<>();
        if (width > 0) {
            networkInfoMap.put("express_width", width);
        }
        if (height > 0) {
            networkInfoMap.put("express_height", height);
        }
        setNetworkInfoMap(networkInfoMap);
    }

    private void bindListener() {
        mTTFeedAd.setExpressRenderListener(new MediationExpressRenderListener() {
            @Override
            public void onRenderFail(View view, String s, int i) {

            }

            @Override
            public void onAdClick() {
                notifyAdClicked();
            }

            @Override
            public void onAdShow() {
                setNetworkInfoMap(AlexGromoreConst.getNetworkMap(mTTFeedAd));
                notifyAdImpression();
            }

            @Override
            public void onRenderSuccess(View view, float v, float v1, boolean b) {

            }
        });
    }

    public void setAdData(boolean canInterrupt) {

        //ttFeedAd.setCanInterruptVideoPlay(canInterrupt);
        int nativeAdInteractionType = NativeAdInteractionType.UNKNOW;
        if (mTTFeedAd.getInteractionType() == 4) {
            nativeAdInteractionType = NativeAdInteractionType.APP_DOWNLOAD_TYPE;
        }
        if (mTTFeedAd.getInteractionType() == 3) {
            nativeAdInteractionType = NativeAdInteractionType.DEEPLINK_TYPE;
        }
        if (mTTFeedAd.getInteractionType() == 2) {
            nativeAdInteractionType = NativeAdInteractionType.H5_TYPE;
        }
        setNativeInteractionType(nativeAdInteractionType);

        mTTFeedAd.setVideoAdListener(new TTFeedAd.VideoAdListener() {
            @Override
            public void onVideoLoad(TTFeedAd ttFeedAd) {

            }

            @Override
            public void onVideoError(int i, int i1) {
                Log.i(TAG, String.format("onVideoError, errorCode: %d, errorMsg: %s",
                        i, i1 + ""));
                notifyAdVideoVideoPlayFail("" + i, "" + i1);
            }

            @Override
            public void onVideoAdStartPlay(TTFeedAd ttFeedAd) {
                notifyAdVideoStart();
            }

            @Override
            public void onVideoAdPaused(TTFeedAd ttFeedAd) {

            }

            @Override
            public void onVideoAdContinuePlay(TTFeedAd ttFeedAd) {

            }

            @Override
            public void onProgressUpdate(long current, long duration) {
                if (getVideoDuration() == 0) {
                    setVideoDuration(duration / 1000D);
                }

                videoProgress = current / 1000D;
                notifyAdVideoPlayProgress((int) videoProgress);
            }

            @Override
            public void onVideoAdComplete(TTFeedAd ttFeedAd) {

            }
        });


        mTTFeedAd.setDownloadListener(new TTAppDownloadListener() {
            @Override
            public void onIdle() {

            }

            @Override
            public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                if (!isDownloadStart) {
                    isDownloadStart = true;
                    if (mDownloadListener != null && mDownloadListener instanceof CustomAdapterDownloadListener) {
                        ((CustomAdapterDownloadListener) mDownloadListener).onDownloadStart(totalBytes, currBytes, fileName, appName);
                    }
                } else {
                    if (mDownloadListener != null && mDownloadListener instanceof CustomAdapterDownloadListener) {
                        ((CustomAdapterDownloadListener) mDownloadListener).onDownloadUpdate(totalBytes, currBytes, fileName, appName);
                    }
                }

            }

            @Override
            public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                if (mDownloadListener != null && mDownloadListener instanceof CustomAdapterDownloadListener) {
                    ((CustomAdapterDownloadListener) mDownloadListener).onDownloadPause(totalBytes, currBytes, fileName, appName);
                }
            }

            @Override
            public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                if (mDownloadListener != null && mDownloadListener instanceof CustomAdapterDownloadListener) {
                    ((CustomAdapterDownloadListener) mDownloadListener).onDownloadFail(totalBytes, currBytes, fileName, appName);
                }
            }

            @Override
            public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                if (mDownloadListener != null && mDownloadListener instanceof CustomAdapterDownloadListener) {
                    ((CustomAdapterDownloadListener) mDownloadListener).onDownloadFinish(totalBytes, fileName, appName);
                }
            }

            @Override
            public void onInstalled(String fileName, String appName) {
                if (mDownloadListener != null && mDownloadListener instanceof CustomAdapterDownloadListener) {
                    ((CustomAdapterDownloadListener) mDownloadListener).onInstalled(fileName, appName);
                }
            }
        });

        switch (mTTFeedAd.getImageMode()) {
            case TTAdConstant.IMAGE_MODE_VIDEO:
            case TTAdConstant.IMAGE_MODE_VIDEO_VERTICAL:
                mAdSourceType = NativeAdConst.VIDEO_TYPE;
                break;

            case TTAdConstant.IMAGE_MODE_GROUP_IMG:
            case TTAdConstant.IMAGE_MODE_LARGE_IMG:
            case TTAdConstant.IMAGE_MODE_SMALL_IMG:
            case TTAdConstant.IMAGE_MODE_VERTICAL_IMG:
                mAdSourceType = NativeAdConst.IMAGE_TYPE;
                break;
        }
    }

    @Override
    public double getVideoProgress() {
        return videoProgress;
    }

    @Override
    public void prepare(View view, ATNativePrepareInfo nativePrepareInfo) {
        if (view != null && view.getContext() instanceof Activity) {
            bindDislike((Activity) view.getContext());
        }
    }

    /**
     * Set ads dislike, developers can customize styles
     */
    private void bindDislike(Activity activity) {
        if (mTTFeedAd == null) {
            return;
        }
        mTTFeedAd.setDislikeCallback(activity, new TTAdDislike.DislikeInteractionCallback() {
            @Override
            public void onShow() {

            }

            @Override
            public void onSelected(int i, String s, boolean b) {
                notifyAdDislikeClick();
            }

            @Deprecated
            public void onSelected(int position, String value) {
                notifyAdDislikeClick();
            }

            @Override
            public void onCancel() {
            }

            @Deprecated
            public void onRefuse() {

            }
        });
    }

    @Override
    public View getAdMediaView(Object... object) {
        if (expressFeedView == null) {
            expressFeedView = mTTFeedAd.getAdView();
        }
        return expressFeedView;
    }

    @Override
    public boolean isNativeExpress() {
        return true;
    }

    @Override
    public void destroy() {
        super.destroy();
        if (mTTFeedAd != null) {
            mTTFeedAd.setExpressRenderListener(null);
            mTTFeedAd.destroy();
        }
    }
}
